#include "librsvg/rsvg.h"
#include "gobject/gobject.h"
#include "svg.h"
#include "cairo.h"
#include <cairo-win32.h>
#include <stdio.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <math.h>
int renderSvgToPng(const char *svgpath, const char *destpath)
{

  GError *error = nullptr;

  RsvgHandle *svgHandle = rsvg_handle_new_from_file(svgpath, &error);

  // rsvg_pixbuf_from_file_at_size
  g_assert_no_error(error);
  // DHS_LOG("==================1,%i",const_cast<char *>(error->message));

  ;

  gdouble w, h;
  rsvg_handle_get_intrinsic_size_in_pixels(svgHandle, &w, &h);
  double scale = 3.0;

  const int width = w * scale;
  const int height = h * scale;

  cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);

  // cairo_surface_reference(surface);

  cairo_t *cairo = cairo_create(surface);
  // cairo_reference(cairo);

  // rsvg_handle_get_dimensions()
  // rsvg_pixbuf_from_file_at_zoom
  RsvgRectangle rsvgRectangle;
  rsvgRectangle.height = h;
  rsvgRectangle.width = w;
  rsvgRectangle.x = 0;
  rsvgRectangle.y = 0;

  cairo_scale(cairo, scale, scale);

  rsvg_handle_render_document(svgHandle, cairo, &rsvgRectangle, &error);

  cairo_surface_write_to_png(surface, destpath);

  cairo_surface_destroy(surface);
  cairo_destroy(cairo);

  g_object_unref(svgHandle);

  return 0;
}

int renderSvgByData(const guint8 *data, gsize data_len, HDC hdc, int cx, int cy, int width, int height)
{

  GError *error = nullptr;

  RsvgHandle *svgHandle = rsvg_handle_new_from_data(data, data_len, &error);

  double w = 0.0, h = 0.0;
  RsvgDimensionData dimension_data;

  rsvg_handle_get_dimensions(svgHandle, &dimension_data);

  w = dimension_data.width;
  h = dimension_data.height;

  cairo_surface_t *surface = cairo_win32_surface_create(hdc);
  cairo_t *cairo = cairo_create(surface);

  // cairo_set_source_rgb(cairo, 1.0, 1.0, 1.0);
  // cairo_set_fill_rule(cairo, CAIRO_FILL_RULE_WINDING);
  // cairo_rectangle(cairo, 0, 0, width, height);
  // cairo_fill(cairo);

  double scale = 1.0;

  cairo_translate(cairo, cx, cy);

  if (height > 0 || width > 0)
  {
    int _h = h, _w = w;

    if (height > 0 && width == 0)
    {
      _h = height;
      _w = _h;
    }
    else if (width > 0 && height == 0)
    {
      _w = width;
      _h = _w;
    }
    else if (width > 0 && height > 0)
    {
      _h = height;
      _w = width;
    }

    scale = _h / h;
    int _ww = scale * w;
    if (_ww > _w)
    {
      scale = _w / w;
    }

    cairo_scale(cairo, scale, scale);
  }

  DHS_LOG("svg_info====svgw=%i,svgh=%i,cx=%i,cy=%i,width=%i,height=%i\n", dimension_data.width, dimension_data.height, cx, cy, width, height);

  RsvgRectangle rsvgRectangle;
  rsvgRectangle.height = h;
  rsvgRectangle.width = w;
  rsvgRectangle.x = 0;
  rsvgRectangle.y = 0;
  rsvg_handle_render_document(svgHandle, cairo, &rsvgRectangle, &error);

  cairo_destroy(cairo);
  cairo_surface_destroy(surface);
  g_object_unref(svgHandle);
  return 0;
}

int getSvgSize(const guint8 *data, gsize data_len, svgSize *out_svgSize)
{

  svgSize *svgSize = out_svgSize;
  if (out_svgSize == nullptr || out_svgSize == nullptr)
  {
    DHS_LOG("===null===指针为空\n");
    return 1;
  }

  GError *error = nullptr;

  RsvgHandle *svgHandle = rsvg_handle_new_from_data(data, data_len, &error);
  gboolean out_has_width;
  RsvgLength out_width;
  gboolean out_has_height;
  RsvgLength out_height;
  gboolean out_has_viewbox;
  RsvgRectangle out_viewbox;
  rsvg_handle_get_intrinsic_dimensions(svgHandle, &out_has_width, &out_width, &out_has_height, &out_height, &out_has_viewbox, &out_viewbox);

  svgSize->u = RSVG_UNIT_PX;

  if (svgSize != nullptr)
  {
    if (out_has_width)
    {
      DHS_LOG("======out_has_width==%f\n", out_width.length);
      svgSize->width = out_width.length;
      svgSize->u = out_width.unit;
    }
    if (out_has_viewbox)
    {
      DHS_LOG("======out_has_viewbox==%f\n", out_viewbox.width);
      svgSize->width = out_viewbox.width;
      svgSize->x = out_viewbox.x;
    }

    if (out_has_height)
    {
      DHS_LOG("======out_has_height==%f\n", out_height.length);
      svgSize->height = out_height.length;
      svgSize->u = out_height.unit;
    }
    if (out_has_viewbox)
    {
      DHS_LOG("======out_has_viewbox==%f\n", out_viewbox.height);
      svgSize->y = out_viewbox.y;
      svgSize->height = out_viewbox.height;
    }
  }

  DHS_LOG("======height=%f,width=%f\n", svgSize->height, svgSize->width);

  g_object_unref(svgHandle);

  return 0;
}

int main(int argc, const char *argv[])
{
  renderSvgToPng("C:/jolong_work/svg-c/test.svg", "test.png");

  return 0;
}
